package com.jianan.mis.app.overseastudy;

import java.io.Serializable;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.app.overseastudy.OsConsultationComposite;
import com.jianan.mis.data.domain.OsConsultation;
import com.jianan.mis.data.domain.OsConsultationState;
import com.jianan.mis.service.OsConsultationService;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.SpringBeanFactory;

public class AddOsConsultationView extends ViewPart {

	public static final String ID = "com.jianan.mis.app.overseastudy.AddOsConsultationView"; //$NON-NLS-1$

	private OsConsultationComposite consultationComposite;
	
	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		parent.setLayout(gridLayout);

		final Label left_label = new Label(parent, SWT.NONE);
		final GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		left_label.setLayoutData(gd_label);

		final ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setAlwaysShowScrollBars(false);
		scrolledComposite.setLayoutData(new GridData(SWT.DEFAULT));

		consultationComposite = new OsConsultationComposite(scrolledComposite, SWT.NONE);
		consultationComposite.setMode(0);
		consultationComposite.setSize(consultationComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolledComposite.setContent(consultationComposite);
		scrolledComposite.setSize(scrolledComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		final Label right_label = new Label(parent, SWT.NONE);
		final GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		right_label.setLayoutData(gd_label_1);
		
		
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	private AddConsultationAction addConsultationAction;
	
	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
		addConsultationAction = new AddConsultationAction(this);
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
		toolbarManager.add(addConsultationAction);
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
		menuManager.add(addConsultationAction);
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
	
	private OsConsultationService osConsultationService;
	
	private void initializeServices() {
		osConsultationService = (OsConsultationService) SpringBeanFactory.getBean("osConsultationService");
	}

	private class AddConsultationAction extends Action {

		public final static String ID = "com.jianan.app.com.jianan.mis.app.overseastudy.addConsultation";

		private ViewPart view;
		
		public AddConsultationAction(ViewPart view) {
			setId(ID);
			setActionDefinitionId(ID);
			setText("保存(&S)");
			setToolTipText("保存");
			setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.SAVE));
			this.view = view;
			initializeServices();
		}

		public void run() {
			try {
				// 保存新的留学咨询表
				OsConsultation osConsultation = consultationComposite
						.getOsConsultation();
				if (osConsultation != null) {
					if (MessageDialog.openConfirm(view.getViewSite()
							.getWorkbenchWindow().getShell(),
							IMessageKeys.CONFIRM, "确认保存该留学咨询表吗?")) {
						osConsultation.setState(OsConsultationState.UNSIGNED);
						Serializable id = osConsultationService
								.save(osConsultation);
						MessageDialog.openInformation(view.getViewSite()
								.getWorkbenchWindow().getShell(),
								IMessageKeys.SUCCESS, "咨询档案号为" + id);
						view.getViewSite().getPage().hideView(view);
					}
				}
			} catch(Exception ex) {
				MessageDialog.openError(view.getViewSite().getWorkbenchWindow().getShell(),
						IMessageKeys.FAIL, ex.getMessage());
			}
		}
	}

}
